package lily.phs.pregnance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import lily.phs.child.ChildScrollActivity;
import lily.phs.child.ImageList;
import lily.phs.child.ScrollLayout;
import lily.phs.child.ChildScrollActivity.myThread;
import lily.phs.database.Constants;
import lily.phs.entertain.YuleActivity;
import lily.phs.epidemic.EpidemicList;
import lily.phs.healthedu.HealthMain;
import lily.phs.object.FirstPrenatalVisit;
import lily.phs.object.ModuleSelect;
import lily.phs.urh.PeopleScrollActivity;
import lily.phs.urh.R;
import lily.phs.vaccination.VaccinationScrollActivity;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 孕产妇档案管理的详细信息，包括4张表：
 * 第1次产前访视记录表（2页）、
 * 第2~5次产前访视记录表、
 * 产后访视记录表、
 * 产后4天健康检查表
 * @author heckassitant
 *
 */
public class PregnantScrollActivity extends Activity  implements OnClickListener,Constants{
	
	
	 private static final String[] nameOfModuleList = {
	    	"城乡居民健康档案管理","健康教育","疫情上报","卫生监督","寓教于乐"};
	 private static final int[] imageOfModuleList = {
	    	R.drawable.peoplehealth,R.drawable.health_education,R.drawable.yiqingshangbao,
	    	R.drawable.weishengjiandu,R.drawable.entertain};
    private static final String[] pregnantTable={"第一次产前随访服务记录表","第2～5次产前随访服务记录表","产后访视记录表","产后42天健康检查记录表"};
	private FirstPrenatalVisitView mFirstPrenatalVisitView;
	private FirstPrenatalVisitView_2 mFirstPrenatalVisitView_2;
	private PrenatalVisitView mPrenatalVisitView;
	private PostnatalVisitView mPostnatalVisitView;
	private LastPostnatalCheckView mLastPostnatalCheckView;
	
	private Button leftBtn,rightBtn,rightBtn2;
	PregnantScrollLayout mRoot;
	private ListView listAccount;
	private GridView gv2;
	private int profileId=0,father,call;
	private int pageIndex;
	private ProgressDialog mpDialog;
	

	@Override
	protected void onCreate(Bundle savedInstanceState){
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.pregnant_scroll_layout);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title);
        
        leftBtn = (Button) findViewById(R.id.title_left_btn);
        rightBtn = (Button) findViewById(R.id.title_right_btn);
        rightBtn2 = (Button) findViewById(R.id.title_right_btn2);
        leftBtn.setText("后退");
        rightBtn.setText("保存");
        rightBtn2.setText("提交");
        leftBtn.setOnClickListener(this);
        rightBtn.setOnClickListener(this);
        rightBtn2.setOnClickListener(this);
        
        mRoot = (PregnantScrollLayout) findViewById(R.id.root);
        listAccount = (ListView) findViewById(R.id.listAccount);
        initList();
        
        gv2 = (GridView) findViewById(R.id.gride2);
        ImageList adapter2 = new ImageList(this,imageOfModuleList,nameOfModuleList);
		gv2.setAdapter(adapter2);
		gv2.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:{//健康档案
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_VACCINATION);
						editprofile.putExtras(bd);
						editprofile.setClass(PregnantScrollActivity.this, PeopleScrollActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_PEOPLE_HEALTH){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(PregnantScrollActivity.this, PeopleScrollActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 1:{//健康教育
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(PregnantScrollActivity.this, HealthMain.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(PregnantScrollActivity.this, HealthMain.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 2:{//疫情上报
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(PregnantScrollActivity.this, EpidemicList.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(PregnantScrollActivity.this, EpidemicList.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
				case 3:{//卫生监督
					
					break;
				}
				case 4:{//寓教于乐
					if(father==MODULE_FATHER){
						Intent editprofile = new Intent();
						Bundle bd = new Bundle();
						bd.putInt("Profile_id", profileId);//待编辑档案的ID
						bd.putInt("father", MODULE_CHILD);
						bd.putInt("call", MODULE_PEOPLE_HEALTH);
						editprofile.putExtras(bd);
						editprofile.setClass(PregnantScrollActivity.this, YuleActivity.class);
						startActivity(editprofile);
						overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
					}
					else if(father==MODULE_CHILD){
						if(call==MODULE_CHILD_MANAGEMENT){
							finish();
							overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
						}
						else{
							Intent editprofile = new Intent();
							Bundle bd = new Bundle();
							bd.putInt("Profile_id", profileId);//待编辑档案的ID
							bd.putInt("father", MODULE_CHILD);
							bd.putInt("call", call);
							editprofile.putExtras(bd);
							editprofile.setClass(PregnantScrollActivity.this, YuleActivity.class);
							startActivity(editprofile);
							overridePendingTransition(R.anim.anim_slide_in_right,R.anim.anim_slide_out_left);
							finish();
						}
					}
					break;
				}
					
				default:
					break;
				}
			}

		});
		
        profileId = getIntent().getExtras().getInt("Profile_id");
        System.out.println("getIntent().getExtras().getInt(Profile_id);"+getIntent().getExtras().getInt("Profile_id"));
        father = getIntent().getExtras().getInt("father");
        call = getIntent().getExtras().getInt("call");
        initViews();

        mRoot.setToScreen(0);
        pageIndex=0;
        
        listAccount.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int index,
					long arg3) {
				// TODO Auto-generated method stub
				switch(index){
				case 0:
					mRoot.setToScreen(0);
					break;
				case 1:
					mRoot.setToScreen(2);
					break;
				case 2:
					mRoot.setToScreen(3);
					break;
				case 3:
					mRoot.setToScreen(4);
					break;
					
				}
			}
        	
        });
        
        mpDialog = new ProgressDialog(PregnantScrollActivity.this);  
		mpDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);//设置风格为圆形进度条  
		mpDialog.setMessage("正在加载...");  
		mpDialog.setIndeterminate(false);//设置进度条是否为不明确  
		mpDialog.setCancelable(true);//设置进度条是否可以按退回键取消  
		mpDialog.show();
		new myThread(handler).start();
        
	}

	private void initViews() {
		// TODO Auto-generated method stub
		mRoot.removeAllViews();
		mFirstPrenatalVisitView = new FirstPrenatalVisitView(this,this.profileId);
//		mFirstPrenatalVisitView.setTag(FIRST_INTENT_TAG);
		mFirstPrenatalVisitView_2 = new FirstPrenatalVisitView_2(this,this.profileId);
//		mFirstPrenatalVisitView_2.setTag(SECOND_INTENT_TAG);
		mPrenatalVisitView = new PrenatalVisitView(this,this.profileId);
//		mPrenatalVisitView.setTag(THIRD_INTENT_TAG);
		mPostnatalVisitView = new PostnatalVisitView(this,this.profileId);
//		mPostnatalVisitView.setTag(FORTH_INTENT_TAG);
		mLastPostnatalCheckView = new LastPostnatalCheckView(this,this.profileId);
//		mLastPostnatalCheckView.setTag(FIFTH_INTENT_TAG);
		
		mRoot.addView(mFirstPrenatalVisitView);
		mRoot.addView(mFirstPrenatalVisitView_2);
		mRoot.addView(mPrenatalVisitView);
		mRoot.addView(mPostnatalVisitView);
		mRoot.addView(mLastPostnatalCheckView);
		
	}

	private void initList() {
		// TODO Auto-generated method stub
		ArrayList<Map<String, Object>> coll= new ArrayList<Map<String, Object>>();
        Map<String, Object> item= new HashMap<String, Object>();;
        item.put("name", "第一次产前随访服务记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "第2~5次产前随访服务记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "产后访视记录表");
        coll.add(item);
        item= new HashMap<String, Object>();;
        item.put("name", "产后42天健康检查记录表");
        coll.add(item);
		listAccount.setAdapter(new SimpleAdapter(this, coll,R.layout.member_listview, new String[] { "name" },new int[] {R.id.txtFather}));
		// 去掉系统自带的按钮
		// 去掉系统自带的分隔线
		listAccount.setDivider(null);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v==leftBtn){
			new AlertDialog.Builder(PregnantScrollActivity.this)
			.setIcon(R.drawable.alert_dialog_icon)
			.setTitle("您确定已经保存了要退出吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int whichButton) {
					PregnantScrollActivity.this.finish();
					overridePendingTransition(R.anim.anim_slide_in_left,R.anim.anim_slide_out_right);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
		                    public void onClick(DialogInterface dialog, int whichButton) {

		                        /* User clicked Cancel so do some stuff */
		                   }
		                })
		                .create().show();
		}
		else if(v==rightBtn){
			pageIndex = mRoot.getCurScreen();
			System.out.println("pageIndex="+pageIndex);
			int i=0;
			switch(pageIndex){
			case 0:
				mFirstPrenatalVisitView.saveViews();
				mFirstPrenatalVisitView_2.saveViews();
				i=0;
				break;
			case 1:
				mFirstPrenatalVisitView.saveViews();
				mFirstPrenatalVisitView_2.saveViews();
				i=0;
				break;
			case 2:
				mPrenatalVisitView.saveViews();
				i=1;
				break;
			case 3:
				mPostnatalVisitView.saveViews();
				i=2;
				break;
			case 4:
				mLastPostnatalCheckView.saveViews();
				i=3;
				break;
			}
			Toast.makeText(this, pregnantTable[i]+"信息保存成功!", Toast.LENGTH_SHORT).show();
		}
		
	}
	
	public class myThread extends Thread{  
		private Handler handler;

		public myThread(Handler handler){
			this.handler=handler;

		}
		
		public void run(){  
	        try  
	        {   
	        	Looper.getMainLooper();
	        	sleep(1000); 
	        	
//	        	mFirstPrenatalVisitView.findViewsById();
	        	mFirstPrenatalVisitView.fetchData();
	        	mFirstPrenatalVisitView_2.findViewsById();
	        	mFirstPrenatalVisitView_2.fetchData();
	        	mPrenatalVisitView.findViewsById();
	        	mPrenatalVisitView.fetchData();
	        	mPostnatalVisitView.findViewsById();
	        	mPostnatalVisitView.fetchData();
	        	mLastPostnatalCheckView.findViewsById();
	        	mLastPostnatalCheckView.fetchData();
	        	
//	        	initGrid();
	        	
	        	Message msg = handler.obtainMessage(); 
	        	msg.what=1;
	        	handler.sendMessage(msg); 
			}  
		    catch (Exception e)  
		    {  
		        e.printStackTrace();  
		    }  
		}  
	}
	
	private final Handler handler = new Handler(Looper.getMainLooper()) {
    	public void handleMessage(Message msg) { 
    		if(msg.what==1){
    			mFirstPrenatalVisitView.initViews();
//    			mFirstPrenatalVisitView.setMyView();
    			mFirstPrenatalVisitView_2.setMyView();
    			mFirstPrenatalVisitView_2.initViews();
    			mPrenatalVisitView.setMyView();
    			mPrenatalVisitView.initViews();
    			mPostnatalVisitView.setMyView();
    			mPostnatalVisitView.initViews();
    			mLastPostnatalCheckView.setMyView();
    			mLastPostnatalCheckView.initViews();
//    			
    			mpDialog.cancel();
    		}
    	} 
    }; 
    

}
